from rest_framework.mixins import UpdateModelMixin, CreateModelMixin, DestroyModelMixin, RetrieveModelMixin
from backend.cews.models import Log
import backend.cews.services.LogService as LogService
import backend.cews.services.TaskService as TaskService
import backend.cews.services.RepairService as RepairService
from django.db import transaction
from utils.decorators import error_response, request_data, activities_logging, check_group_permission, check_stock_item_group
import json

class RepairOrderPartVersionUpdateModelMixin(UpdateModelMixin):
    @activities_logging(
        log_type=Log.LogType.MAINTENANCE,
        category_type=Log.CategoryType.VERSION,
        action_type=Log.ActionType.UPDATE,
        action="RepairOrderPartVersionUpdateModelMixin",
        description="update repair order part version"
    )
    @error_response()
    def update(self,request,*args,**kwargs):
        return super().update(request,*args,**kwargs)
    
class StockItemTaskUpdateModelMixin(UpdateModelMixin):
    @activities_logging(
        log_type=Log.LogType.STOCKMASTER,
        category_type=Log.CategoryType.TASK,
        action_type=Log.ActionType.UPDATE,
        action="StockItemTaskUpdateModelMixin",
        description="update stock item task"
    )
    @error_response()
    @check_group_permission('change_stockitemtask')
    def update(self,request,*args,**kwargs):
        return super().update(request,*args,**kwargs)
    
class StockItemTaskCreateModelMixin(CreateModelMixin):
    @activities_logging(
        log_type=Log.LogType.STOCKMASTER,
        category_type=Log.CategoryType.TASK,
        action_type=Log.ActionType.CREATE,
        action="StockItemTaskCreateModelMixin",
        description="create stock item task"
    )
    @error_response()
    @check_group_permission('add_stockitemtask')
    def create(self,request,*args,**kwargs):
        return super().create(request,*args,**kwargs)
    
class StockItemTaskDeleteModelMixin(DestroyModelMixin):
    @activities_logging(
        log_type=Log.LogType.STOCKMASTER,
        category_type=Log.CategoryType.TASK,
        action_type=Log.ActionType.DELETE,
        action="StockItemTaskDeleteModelMixin",
        description="delete stock item task"
    )
    @error_response()
    @check_group_permission('delete_stockitemtask')
    def destroy(self,request,*args,**kwargs):
        return super().destroy(request,*args,**kwargs)
    
class PartUpdateModelMixin(UpdateModelMixin):
    @activities_logging(
        log_type=Log.LogType.PARTMASTER,
        category_type=Log.CategoryType.PART,
        action_type=Log.ActionType.UPDATE,
        action="PartUpdateModelMixin",
        description="update part item"
    )
    @error_response()
    def update(self,request,*args,**kwargs):
        return super().update(request,*args,**kwargs)
    
class StockItemEdocUpdateModelMixin(UpdateModelMixin):
    @activities_logging(
        log_type=Log.LogType.STOCKMASTER,
        category_type=Log.CategoryType.EDOC,
        action_type=Log.ActionType.UPDATE,
        action="StockItemEdocUpdateModelMixin",
        description="update stock item edoc"
    )
    @error_response()
    @check_group_permission('change_stockitemedoc')
    def update(self,request,*args,**kwargs):
        return super().update(request,*args,**kwargs)
    
class TaskUpdateModelMixin(UpdateModelMixin):
    # @activities_logging(
    #     log_type=Log.LogType.MAINTENANCE,
    #     category_type=Log.CategoryType.TASK,
    #     action_type=Log.ActionType.UPDATE,
    #     action="TaskCreateModelMixin",
    #     description="update repair order task"
    # )
    @error_response()
    @check_group_permission('change_task')
    def update(self,request,*args,**kwargs):
        return super().update(request,*args,**kwargs)
    
class TaskCreateModelMixin(CreateModelMixin):
    # @activities_logging(
    #     log_type=Log.LogType.MAINTENANCE,
    #     category_type=Log.CategoryType.TASK,
    #     action_type=Log.ActionType.CREATE,
    #     action="TaskCreateModelMixin",
    #     description="create repair order task"
    # )
    @error_response()
    @check_group_permission('add_task')
    def create(self,request,*args,**kwargs):
        return super().create(request,*args,**kwargs)
    

class RepairOrderModelMixin(RetrieveModelMixin):
    @error_response()
    @check_stock_item_group()
    def retrieve(self,request,*args,**kwargs):
        return super().retrieve(request,*args,**kwargs)
    
class RepairOrderUpdateModelMixin(UpdateModelMixin):
    @activities_logging(
        log_type=Log.LogType.MAINTENANCE,
        category_type=Log.CategoryType.ORDER,
        action_type=Log.ActionType.UPDATE,
        action="RepairOrderUpdateModelMixin",
        description="update repair order"
    )
    @error_response()
    @transaction.atomic
    def update(self,request,*args,**kwargs):
        ## update repair order task after update repair order's mtce type 
        repair_order_id = kwargs['pk']
        print(request.data)
        mtce_type = request.data.get('mtce_type')
        dlc_no = request.data.get('dlc_form_no')
        contractor_liability = request.data.get('contractor_liability')

        TaskService.check_mtce_type_and_update_order_task(repair_order_id, mtce_type, request)
        RepairService.update_repair_order_inspection(repair_order_id, contractor_liability, dlc_no)
        return super().update(request,*args,**kwargs)
    

### improvement work
class StockItemImpWorkUpdateModelMixin(UpdateModelMixin):
    @activities_logging(
        log_type=Log.LogType.STOCKMASTER,
        category_type=Log.CategoryType.IMPWORK,
        action_type=Log.ActionType.UPDATE,
        action="StockItemImpWorkUpdateModelMixin",
        description="update stock item improvement work"
    )
    @error_response()
    @check_group_permission('change_stockitemimprovementwork')
    def update(self,request,*args,**kwargs):
        return super().update(request,*args,**kwargs)
    
class StockItemImpWorkDeleteModelMixin(DestroyModelMixin):
    @activities_logging(
        log_type=Log.LogType.STOCKMASTER,
        category_type=Log.CategoryType.IMPWORK,
        action_type=Log.ActionType.DELETE,
        action="StockItemImpWorkDeleteModelMixin",
        description="delete stock item improvement work"
    )
    @error_response()
    @check_group_permission('delete_stockitemimprovementwork')
    def destroy(self,request,*args,**kwargs):
        return super().destroy(request,*args,**kwargs)